javascript - 通过 js 在 R 中设置默认 textInput 值(作为 js 函数的结果)
问题描述
我想将 R 闪亮的应用程序与一个社交网络的 js SDK 一起使用。我想从 js API 获取用户 ID 并将其设置为 textInput 表单的默认值。但只有一次,第一次。
我设法使用Shiny.onInputChange()
功能(或Shiny.setInputValue()
闪亮> 1.1)实现的最佳结果
玩具示例:
ui <- fluidPage(
textInput("uid",label = h4("Input ID"),value = "1"),
actionButton("goButton", "Check this out!", class="btn-primary"),
# getting user id via js
tags$script(HTML(
'
uid = some_js_code;
console.log("My uid - " + uid);
// setting new value for input$uid variable
Shiny.onInputChange("uid", uid);
// for newer version of shiny
//Shiny.setInputValue("uid", uid);
'
)
server <- function(input, output, session) {
user<-reactive({
input$goButton
user <- some_function_for_uid(input$uid)
})
}
问题:
在第一次加载应用程序时变量“uid”的值没有改变。
value="1"
该值与 textInput 函数 ( )中的值保持一致只有当我按下 goButton 时,服务器函数才会
some_function_for_uid()
接收变量的新值。但是文本形式的值仍然保持不变。
如何正确更改 textInput 中的默认值并避免描述的问题?先感谢您。
解决方案
要更新 中的值textInput
:
$("#uid").val(uid);
我不知道你想用这个按钮做什么。像这样可以吗:
ui <- fluidPage(
textInput("uid", label = h4("Input ID"), value = "1"),
verbatimTextOutput("showUID"),
#actionButton("goButton", "Check this out!", class="btn-primary"),
tags$script(HTML(
'
uid = "hello";
// setting new value in the textInput
$("#uid").val(uid);
// setting new value for input$uid variable
Shiny.onInputChange("uid", uid);
'
))
)
server <- function(input, output, session) {
user <- eventReactive(input$uid, {
rep(input$uid, 3)
})
output[["showUID"]] <- renderText({user()})
}
推荐阅读
- angular - 如何导入isDate?
- java - 这行包含“for”循环的代码背后的逻辑解释是什么?
- zip - 无法使用 Zip 命令
- javascript - 如何将文本放在选择类中?
- jquery - Sticky Header 动画在滚动时弹跳
- ios - Firestore 侦听器获取的数据多于现有文档的数量
- java - 检测到不一致。无效的项目位置 0(偏移量:-1)... RecyclerView 在删除第一个项目时崩溃
- java - 将每个像素的颜色值存储到二维数组中
- r - purrr::pmap 用于具有多个输入和多个返回值的函数
- javascript - 将 div 居中在另一个 div 中的最佳方法是什么?