这是一个简单的应用程序,用户可以在其中键入(numericInput,bpm_numeric)或滑动(sliderInput,bpm_slider)输入值。这两个UI元素均应与任一选择保持同步。但是,输出只需要跟踪一次。在下面,我对其进行了两次跟踪,但是我只需要用户提供一份“选择/输入”。我认为我需要使用reactiveValues
,但是我不确定这种方法。
library(shiny)
ui <- fluidPage(
titlePanel("Reverb & Delay Calculator"),
sidebarLayout(
sidebarPanel(
uiOutput("bpm_numeric"),
uiOutput("bpm_slider")
),
mainPanel(
p("Numeric value is:"),
verbatimTextOutput("numeric_val"),
p("Slider value is:"),
verbatimTextOutput("slider_val")
)
)
)
server <- function(input, output, session) {
output$bpm_numeric = renderUI({
numericInput(
"bpm_numeric",
"BPM Numeric",
value = 60, min = 40, max = 300
)
})
output$bpm_slider = renderUI({
sliderInput(
"bpm_slider",
"BPM Slider",
value = 60, min = 40, max = 300
)
})
# See how I can track "both", but really I only need to track "one" since
# they should stay in-sync with one another. Maybe reactiveValues???
output$numeric_val <- renderText({ input$bpm_numeric })
output$slider_val <- renderText({ input$bpm_slider })
observeEvent(input$bpm_numeric, {
val = input$bpm_numeric
updateSliderInput(session, "bpm_slider", value = val)
})
observeEvent(input$bpm_slider, {
val = input$bpm_slider
updateNumericInput(session, "bpm_numeric", value = val)
})
}
shinyApp(ui = ui, server = server)
注意:当我当前正在使用uiOuput()
+ renderUI()
+ update___Input()
时,这不一定是必需的。我对其他方法持开放态度,只要输入UI保持同步,并且我拥有同步输出的一个副本即可。
答案 0 :(得分:1)
这是一个想法。它可以泛化一点,但是它仍然依赖于您完全知道要以这种方式组合哪些id。
对您当前示例的修改:
function cell_test() {
try {
// Increment i
var props = PropertiesService.getScriptProperties();
var i = props.getProperty("Test");
if( i === null ) {
i = 8;
}
else {
i++;
}
props.setProperty("Test", i);
// Put your code here
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("A!C1");
var destSheet = ss.getSheetByName("B");
var destRange = destSheet.getRange(i,2);
source.copyTo (destRange, {contentsOnly: true});
}
catch(err) {
Logger.log(err);
}
}
,有助于确保所有元素都在同一位置开始。import datetime
date_string = '04-23-2019'
try:
date = datetime.datetime.strptime(date_string, '%m-%d-%Y')
except ValueError as err:
print(err)
else:
print(date.year)
print(date.month)
print(date.month)
反应式之外,将对defbmp <- 60
或useval
的 all 引用替换为input$bpm_numeric
。_slider
反应值。useval()
块取决于每个要同步的元素。最终产品(不包括lastval
组件,在那里没有任何更改):
useval <- eventReactive({},{})
如何改善这一点(目前我还不知道):
ui
个ID向量; defbpm <- 60 # new
server <- function(input, output, session) {
output$bpm_numeric = renderUI({
numericInput(
"bpm_numeric",
"BPM Numeric",
value = defbpm, min = 40, max = 300 # update
)
})
output$bpm_slider = renderUI({
sliderInput(
"bpm_slider",
"BPM Slider",
value = defbpm, min = 40, max = 300 # update
)
})
output$numeric_val <- renderText({ useval() }) # update
output$slider_val <- renderText({ useval() }) # update
lastval <- shiny::reactiveVal(defbpm) # new
useval <- eventReactive({
input$bpm_numeric
input$bpm_slider
}, {
newval <- setdiff(c(input$bpm_numeric, input$bpm_slider), lastval())
if (length(newval) > 0) {
if (newval != input$bpm_numeric) updateNumericInput(session, "bpm_numeric", value = newval)
if (newval != input$bpm_slider) updateSliderInput(session, "bpm_slider", value = newval)
lastval(newval)
}
lastval()
}) # new
}
以通过编程方式生成条件表达式,目前我还不足以做到这一点。和character
还是eventReactive
(或其他),以便可以更一般地调用numericInput
函数。