如何访问在服务器上创建的 inputId 的 ID?
How to access the id of an inputId created on the server?
我有一个应用程序,当我将 10 添加到 sliderInput
时,会出现一个“确定”按钮。但是,它的 inputId
在 DOM 中不可访问,因为 #sbtn1
是在 server
中创建的。我需要它是因为我在应用程序中需要它。
我想访问此 inputId
(#sbtn1
) 以通过 JS
(file.js
) 修改它,但它不存在。
我的应用程序:
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
dropdownMenuOutput(
outputId = "drop1"
)
)
sidebar <- dashboardSidebar(
width = 300
)
jscode <- "$(function() {
$('.skin-blue').addClass('sidebar-mini');
});
"
body <- dashboardBody(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode, functions = "JS"),
sliderInput(
inputId = "one",
label = "Registro 1",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "two",
label = "Registro 2",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
valueBoxOutput(
outputId = "box1"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(session, input, output) {
fsoma <- function(x) {
if (x != 10) {
"Error!"
} else
(actionButton(
inputId = "sbtn1",
label = "OK",
icon = icon("play-circle-o"),
style = "color: #f2f2f2; background-color: #333333;
border-color: #333333;"
))
}
fx <- function(x, y) {
x + y
}
fy <- function(x) {
x
}
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
chuveiro <- reactive({
temp <- reac_0()
fx(
x = temp$one,
y = temp$two
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
output$box1 <- renderValueBox({
expr = valueBox(
value = fsoma(x = chuveiro()),
subtitle = "Sum"
)
})
}
shinyApp(ui, server)
如何访问此 ID (#sbtn1
) 以在我的 file.js
文档中处理它?
我在按钮后绑定脚本,所以每次创建按钮后脚本都会运行。
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
dropdownMenuOutput(
outputId = "drop1"
)
)
sidebar <- dashboardSidebar(
width = 300
)
body <- dashboardBody(
sliderInput(
inputId = "one",
label = "Registro 1",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "two",
label = "Registro 2",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
valueBoxOutput(
outputId = "box1"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(session, input, output) {
fsoma <- function(x) {
if (x != 10) {
"Error!"
} else {
div(
actionButton(
inputId = "sbtn1",
label = "OK",
icon = icon("play-circle-o"),
style = "color: #f2f2f2; background-color: #333333; border-color: #333333;"
),
tags$script('$(function() {console.log(1); $("#sbtn1").css("background-color", "red");})')
)
}
}
fx <- function(x, y){ x + y}
fy <- function(x){x}
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
chuveiro <- reactive({
temp <- reac_0()
fx(
x = temp$one,
y = temp$two
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
output$box1 <- renderValueBox({
valueBox(
value = fsoma(x = chuveiro()),
subtitle = "Sum"
)
})
}
shinyApp(ui, server)
我有一个应用程序,当我将 10 添加到 sliderInput
时,会出现一个“确定”按钮。但是,它的 inputId
在 DOM 中不可访问,因为 #sbtn1
是在 server
中创建的。我需要它是因为我在应用程序中需要它。
我想访问此 inputId
(#sbtn1
) 以通过 JS
(file.js
) 修改它,但它不存在。
我的应用程序:
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
dropdownMenuOutput(
outputId = "drop1"
)
)
sidebar <- dashboardSidebar(
width = 300
)
jscode <- "$(function() {
$('.skin-blue').addClass('sidebar-mini');
});
"
body <- dashboardBody(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode, functions = "JS"),
sliderInput(
inputId = "one",
label = "Registro 1",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "two",
label = "Registro 2",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
valueBoxOutput(
outputId = "box1"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(session, input, output) {
fsoma <- function(x) {
if (x != 10) {
"Error!"
} else
(actionButton(
inputId = "sbtn1",
label = "OK",
icon = icon("play-circle-o"),
style = "color: #f2f2f2; background-color: #333333;
border-color: #333333;"
))
}
fx <- function(x, y) {
x + y
}
fy <- function(x) {
x
}
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
chuveiro <- reactive({
temp <- reac_0()
fx(
x = temp$one,
y = temp$two
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
output$box1 <- renderValueBox({
expr = valueBox(
value = fsoma(x = chuveiro()),
subtitle = "Sum"
)
})
}
shinyApp(ui, server)
如何访问此 ID (#sbtn1
) 以在我的 file.js
文档中处理它?
我在按钮后绑定脚本,所以每次创建按钮后脚本都会运行。
library(shiny)
library(shinydashboard)
header <- dashboardHeader(
title = "Dashboard",
titleWidth = 300,
dropdownMenuOutput(
outputId = "drop1"
)
)
sidebar <- dashboardSidebar(
width = 300
)
body <- dashboardBody(
sliderInput(
inputId = "one",
label = "Registro 1",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
sliderInput(
inputId = "two",
label = "Registro 2",
value = 1,
animate = animationOptions(
interval = 500, loop = TRUE
),
min = 1,
max = 10,
step = 1,
ticks = TRUE
),
valueBoxOutput(
outputId = "box1"
)
)
ui <- dashboardPage(
header = header,
sidebar = sidebar,
body = body
)
server <- function(session, input, output) {
fsoma <- function(x) {
if (x != 10) {
"Error!"
} else {
div(
actionButton(
inputId = "sbtn1",
label = "OK",
icon = icon("play-circle-o"),
style = "color: #f2f2f2; background-color: #333333; border-color: #333333;"
),
tags$script('$(function() {console.log(1); $("#sbtn1").css("background-color", "red");})')
)
}
}
fx <- function(x, y){ x + y}
fy <- function(x){x}
reac_0 <- reactive({
tibble::tibble(
one = input$one,
two = input$two,
three = input$three
)
})
chuveiro <- reactive({
temp <- reac_0()
fx(
x = temp$one,
y = temp$two
)
})
luz <- reactive({
temp <- reac_0()
fy(
x = temp$three
)
})
output$box1 <- renderValueBox({
valueBox(
value = fsoma(x = chuveiro()),
subtitle = "Sum"
)
})
}
shinyApp(ui, server)