我对Shiny还是陌生的,但我仍在围绕语法进行思考。我正在创建一个应用,其中我想使用范围滑块的值来裁剪栅格。但是,我不知道如何将其集成到渲染函数之外。
例如,如果我有一个简单的10x10栅格,其值从1:100开始,我想只将范围保持在滑块中。手动使用设置的数字(例如20-40)来执行此操作,
r<-raster(ncol = 10, nrow = 10)
values(r) <- 1:ncell(r)
plot(r)
r[r <= 20] <- NA
r[r >=40] <- NA
plot(r)
但是我似乎无法弄清楚如何将滑块变量集成到这种类型的设置中。我添加了renderText以确保我正确地引用了这些值,但是我不知道如何在渲染函数之外使用这些值。
ui <- fluidPage(
sliderInput("range","range:", min = 1, max = 100, value = c(10,50)),
plotOutput("original"),
textOutput("range"),
plotOutput("cropped")
)
server<- function(input, output){
r<-raster(ncol = 10, nrow = 10)
values(r) <- 1:ncell(r)
r2<-r
reactive({r2[r2 <= input$range[1]] <- NA})
reactive({r2[r2 >= input$range[2]] <- NA})
output$original<- renderPlot(plot(r))
output$cropped<- renderPlot(plot(r2))
range <- reactive({cbind(input$range[1], input$range[2])})
output$range<- renderText({range()})
}
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
要使变量r和r2监听滑块中的更改,必须使其具有反应性:
public class News {
private String mSectionName;
private String mWebPublicationDate;
private String mWebUrl;
private String mHeadline;
private String mThumbnail;
private String mWebTitle;
private String mPillarName;
public News(String mSectionName, String mWebPublicationDate, String mWebUrl, String mHeadline, String mThumbnail, String mWebTitle, String mPillarName) {
this.mSectionName = mSectionName;
this.mWebPublicationDate = mWebPublicationDate;
this.mWebUrl = mWebUrl;
this.mHeadline = mHeadline;
this.mThumbnail = mThumbnail;
this.mWebTitle = mWebTitle;
this.mPillarName = mPillarName;
}
public String getmSectionName() {
return mSectionName;
}
public String getmWebPublicationDate() {
return mWebPublicationDate;
}
public String getmWebUrl() {
return mWebUrl;
}
public String getmHeadline() {
return mHeadline;
}
public String getmThumbnail() {
return mThumbnail;
}
public String getmWebTitle() {
return mWebTitle;
}
public String getmPillarName() {
return mPillarName;
}
@Override
public String toString() {
return "News{" +
"mSectionName='" + mSectionName + '\'' +
", mWebPublicationDate='" + mWebPublicationDate + '\'' +
", mWebUrl='" + mWebUrl + '\'' +
", mHeadline='" + mHeadline + '\'' +
", mThumbnail='" + mThumbnail + '\'' +
", mWebTitle='" + mWebTitle + '\'' +
", mPillarName='" + mPillarName + '\'' +
'}';
}
}