我有一个问题更属于“最佳实践”类型的查询。使用shinypackage in 时r,是否最好在server侧面使用创建所有小部件renderUI,然后将它们推送到uivia uiOutput?或者,如果可能,是否应该在ui侧面创建所有小部件?
例如,下面的两个应用程序做同样的事情,但在第二个应用程序中,我sliderInput在server侧面创建,然后将其推送到 ,ui而不是在ui侧面创建。(注意,此代码是从R Studio 上的Hello Shiny页面中提取的)
ui#ui.R
# Define UI for application that plots random distributions
library(shiny)
ui1 <- shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
# Sidebar with a slider input for number of observations
sidebarLayout(
sidebarPanel(
sliderInput("obs",
"Number of observations:",
min = 1,
max = 1000,
value = …Run Code Online (Sandbox Code Playgroud) 我想使用ggplot2像这样的上三角相关矩阵.我可以很好地复制那个,但由于某种原因,我仍然坚持要将reshape2函数转换为tidyr1.我认为我可以gather代替使用melt,但这不起作用.
reshape2library(reshape2)
library(ggplot2)
mydata <- mtcars[, c(1,3,4,5,6,7)]
cormat <- round(cor(mydata),2)
library(reshape2)
melted_cormat <- melt(cormat)
# Get upper triangle of the correlation matrix
get_upper_tri <- function(cormat){
cormat[lower.tri(cormat)]<- NA
return(cormat)
}
upper_tri <- get_upper_tri(cormat)
melted_cormat <- melt(upper_tri, na.rm = TRUE)
ggplot(data = melted_cormat, aes(Var2, Var1, fill = value)) +
geom_tile()
Run Code Online (Sandbox Code Playgroud)
gather来自tidyr.library(tidyverse)
#first correlatoin matrix
cor_base <- round(cor(mydata), 2)
#now UT
cor_base[lower.tri(cor_base)] <- NA …Run Code Online (Sandbox Code Playgroud) 我有一个数字向量,表示自 1970 年 1 月 1 日以来的毫秒数。我想使用 将它们转换为日期时间对象lubridate。数据示例如下:
raw_times <- c(1139689917479, 1139667123031, 1140364113915, 1140364951003,
1139643685434, 1139677091970, 1139691963511, 1140339448413, 1140368308429,
1139686613641, 1139666081813, 1140351488730, 1140346617958, 1141933663183,
1141933207579, 1140360125149, 1140351845108, 1140365079103, 1141933549825,
1140365601476)
Run Code Online (Sandbox Code Playgroud)
as_date知道和 的文档表明它们采用表示自 1970 年 1 月 1 日以来的天as_datetime数的数字向量,我尝试了以下操作:
library(lubridate)
as_date(raw_times / (1000 * 60 * 60 * 24))
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-11" "2006-02-19" "2006-02-19" "2006-02-11"
"2006-02-11" "2006-02-19" "2006-02-19" "2006-03-09" "2006-03-09"
"2006-02-19" "2006-02-19" "2006-02-19" "2006-03-09" "2006-02-19"
Run Code Online (Sandbox Code Playgroud)
(显然是利用这样的事实:一秒有 1000 毫秒,一分钟有 60 秒,一小时有 60 …
data.table 是惊人的。
我想做一个就地连接,但要保留两个表中的所有列。这个问题演示了如何为单个列执行此操作。当我希望连接表中的所有列都包含在最终结果中并在一个内存位置完成所有操作时,我该如何概括这一点。
library(data.table)
dt1 <- data.table(col1 = c("a", "b", "c"),
col2 = 1:3,
col3 = c(TRUE, FALSE, FALSE))
setkey(dt1, col1)
set.seed(1)
dt2 <- data.table(col1 = sample(c("a", "b", "c"), size = 10, replace = TRUE),
another_col = sample(1:10, size = 10, replace = TRUE),
and_anouther = sample(c(TRUE, FALSE), size = 10, replace = TRUE))
setkey(dt2, col1)
# I want to stick the columns from dt1 onto dt2
# this works
dt3 <- dt2[dt1]
dt3
col1 another_col and_anouther col2 …Run Code Online (Sandbox Code Playgroud) 我想用shiny和创建一个交互式绘图ggplot2。我已经能够与成功做到这一点geom_point和其他geoms明显带有x和y轴。geom_bar但是,当使用 a 之类的东西时,这更难,因为您没有y变量。
这里和这里有一个解决方案,可以x从点击事件中提取变量来进行所需的过滤,但这些都没有处理带有刻面的图。我想在ggplot带有刻面的a 上使用单击选项。我试图在第一个链接处修改代码,但这并没有成功。
library(ggplot2)
library(shiny)
ui <- fluidPage(
fluidRow(
plotOutput("plot1", height = 300, width = 300,
click = "plot1_click",
)
),
verbatimTextOutput("x_value"),
verbatimTextOutput("selected_rows")
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(ToothGrowth, aes(supp)) + geom_bar(stat = "count") + facet_wrap(~dose)
})
# Print the name of the x value
output$x_value <- renderPrint({
if (is.null(input$plot1_click$x)) return() …Run Code Online (Sandbox Code Playgroud)